import { ref, reactive, onMounted } from "vue";
// 导入获取用户列表接口
import { getUserList } from "@/api/userAPI";
export const useDataList = () => {
  // 定义用户列表
  const data = ref([]);

  // 表格分页
  const tablePagination = reactive({
    current: 1,
    pageSize: 10,
    pageSizeOptions: ["10"],
    total: 0,
  });

  // 获取用户列表数据
  const getUserData = () => {
    // 参数
    const query = {
      current: tablePagination.current,
      pageSize: tablePagination.pageSize,
    };

    // 发请求获取用户列表
    getUserList(query)
      .then((res) => {
        data.value = res.data;
        tablePagination.total = res.total;
        // 判断是否超出最后一页，如果超出，重新请求
        if (res.current > res.pages && res.pages != 0) {
          tablePagination.current = res.pages;
          getUserData();
        }
      })
      .catch((error) => {
        throw new Error(error);
      });
  };

  onMounted(() => {
    getUserData();
  });

  return {
    data,
    tablePagination,
    getUserData,
  };
};
